package jianzhiOffer;

/**
 * 求 1+2+...+n ，要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句（A?B:C）。
 */
public class T00064 {
    public static void main(String[] args) {
        System.out.println(new Solution().sumNums(0));
    }

static class Solution {
    public int sumNums(int n) {
        // 1 + 2 + 3 + ... + n = n * (n + 1) / 2
        int mul = 0;
        // n + 1
        int n1 = n + 1;

        boolean b;
        // calc n * (n + 1)
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);
        n <<= 1;
        n1 >>= 1;
        b = ((n1 & 0b1) == 1) && ((mul += n) > 0);

        // ÷2
        return mul >> 1;
    }
}
}